package dao.gestion.orm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class Conexion {

	private static Conexion instancia = null;

	private Connection conexion;

	private static String ruta;

	private Conexion() {
	};

	private synchronized static void crearInstancia() {
		if (instancia == null) {
			instancia = new Conexion();
			ruta = "/SQLite/gestion.db";
		}
	}

	public static Conexion getInstancia() {
		crearInstancia();
		return instancia;
	}

	public void conectar() {
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		try {
			conexion = DriverManager.getConnection("jdbc:sqlite:" + ruta);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}
	}

	public void desconectar() {
		try {
			conexion.close();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}
	}

	public Connection getConexion() {
		return conexion;
	}

}
